package com.itheima.mp.domain.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.po.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class PageQuery {
    @ApiModelProperty("页码")
    private Integer pageNo;
    @ApiModelProperty("页大小")
    private Integer pageSize;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc = true;

    public <T> Page<T> toMpPage(OrderItem... orderItems) {
        //1.构建分页条件
        //1.1构建查询条件
        Page<T> page = Page.of(pageNo,pageSize);
        //1.2排序条件
        if(StrUtil.isNotEmpty(sortBy)){
            page.addOrder(new OrderItem(sortBy,isAsc));
        }else if(orderItems != null){
            //为空，默认按照更新时间排序
            page.addOrder(orderItems);
        }
        return page;
    }

    public <T> Page<T> toMpPage(String defaultSortBy,Boolean defaultAsc) {
        return toMpPage(new OrderItem(defaultSortBy,defaultAsc));
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTime(){
        return toMpPage(new OrderItem("create_time",isAsc));
    };
}
